package com.mxdx.edu.controller;

import com.mxdx.base.service.facade.IFileUploadServiceFacade;
import com.mxdx.edu.common.ApiResult;
import com.mxdx.edu.common.log.AppLogger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class UploadFileController extends BaseController {
    private AppLogger logger = new AppLogger(UploadFileController.class);
    @Resource
    private IFileUploadServiceFacade fileUploadServiceFacade;

    @RequestMapping(value = "/teacher/auth/uploadPic", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> handleUploadProcess(HttpServletRequest request) {
        logger.entry(request.getRemoteHost(), "handleUploadProcess");
        Map<String, Object> logMap = new HashMap<>();
        Map<String, Object> returnMap = new HashMap<>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String fileName = "";
        try {
            for (Iterator<?> it = multipartRequest.getFileNames(); it.hasNext();) {
                String key = (String) it.next();
                List<MultipartFile> mulfiles = multipartRequest.getFiles(key); // 一个上传控件上传多张图片
                for (MultipartFile file : mulfiles) {
                    if (file.getSize() == 0) {
                        returnMap.put("msg", "请选择上传文件");
                        return returnMap;
                    }
                    String name = file.getOriginalFilename();
                    String suffix = name.substring(name.lastIndexOf(".") + 1,
                            name.length());
                    logMap.put("name", name);
                    logMap.put("suffix", suffix);
                    ApiResult<Map<String, String>> ret = fileUploadServiceFacade
                            .saveFile("mxdx", suffix, file.getInputStream());
                    String status = ret.getStatus();
                    logMap.put("status", status);
                    logger.doInfo("上传文件结果", ret);
                    if ("00000000".equals(status)) {
                        fileName = ret.getResult().get("saveFilename");
                    }
                }
            }
            returnMap.put("msg", "success");
            returnMap.put("fileName", fileName);
            returnMap.put("imageUrl",
                    String.format("%s%s", imageHost, fileName));

            logMap.putAll(returnMap);
            logger.info(logMap);

            return returnMap;
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("msg", "failed");
            logMap.putAll(returnMap);
            logger.error(logMap);
            return returnMap;
        }
    }
}